package com.guan.controller;

import com.guan.core.BaseController;
import com.guan.core.CustomView;
import com.guan.core.annotation.Autowired;
import com.guan.core.annotation.Controller;
import com.guan.core.annotation.RequestMapping;
import com.guan.core.annotation.ResponseBody;
import com.guan.core.entity.PageData;
import com.guan.service.AdminService;
import com.guan.util.Md5Util;
import com.guan.util.SystemUtil;
import com.guan.util.ValidateCodeUtil;
import org.apache.log4j.Logger;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

/**
 * 商场后台控制器
 * Created by Administrator on 2018/1/21 0021.
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController
{
    protected Logger log = Logger.getLogger(AdminController.class);
    @Autowired("adminService")
    private AdminService adminService;
    /**
     * 进入登录页
     * @return
     */
    @RequestMapping("/index")
    public CustomView login(){
        return new CustomView("/admin/login");
    }

    /**
     * 登录校验
     * @return
     */
    @RequestMapping("/doLogin")
    public CustomView doLogin(){
        log.debug("进入登录校验");
        PageData pd = this.getPageDate();
        String username = pd.getString("username");
        String password = pd.getString("password");
        String code = pd.getString("code");
        CustomView customView = null;
        Object obj = this.getSeesionAttr("code");
        if(null == obj){
            customView = new CustomView("/admin/login");
            return customView;
        }
        String codeSession = obj.toString();
        Map<String,Object> res = adminService.doLogin(username, Md5Util.encodeMd5(username+SystemUtil.USERENCODE+password));
        if(!code.equalsIgnoreCase(codeSession)){
            put("username",username);
            put("password",password);
            put("msg","验证码填写错误！");
            customView = new CustomView("/admin/login");
            return customView;
        }
        if(res.isEmpty()){
            put("username",username);
            put("password",password);
            put("msg","用户名或者密码错误！");
            customView = new CustomView("/admin/login");
        }else{
            SystemUtil.setAdmin(this.getReq(),res);
            customView = new CustomView("/admin/index");
        }
        log.debug("登录校验结束");
        return customView;
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("/logout")
    public CustomView loginOut(){
        HttpServletRequest request = getReq();
        request.getSession().invalidate();
        return new CustomView("/admin/login");
    }

    /**
     * 验证码生成
     */
    @RequestMapping("/code")
    @ResponseBody
    public void code(){
        BufferedImage image = ValidateCodeUtil.getValidateCode(getReq());
        try {
            OutputStream outputStream = getRep().getOutputStream();
            ImageIO.write(image, "JPEG",outputStream);
            outputStream.close();
        } catch (IOException e) {
            log.error("验证码生成错误");
        }
    }
    @RequestMapping("/validateCode")
    @ResponseBody
    public void validateCode(){
        String code = getPageDate().getString("code");
        String codeSession = this.getSeesionAttr("code").toString();
        boolean flag = false;
        if(code.equalsIgnoreCase(codeSession)){
            flag = true;
        }
        writerToResponse(flag+"");
    }
}
